namespace ST_CSharp_初始化器
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ObjectInitializer();
            AnonymousInitializer();
            CollectionInitializer();
        }

        /// <summary>
        /// 对象的初始化器
        /// </summary>
        private static void ObjectInitializer()
        {
            StudentName student1 = new StudentName("Wang", "Meimei");
            StudentName student2 = new StudentName { FirstName = "Li", LastName = "Leilei" };
            StudentName student3 = new StudentName { Id = 3 };
            StudentName student4 = new StudentName("Zhang", "Xiaoshuai") { Id = 4 };
            Console.WriteLine(student1);
            Console.WriteLine(student2);
            Console.WriteLine(student3);
            Console.WriteLine(student4);
        }

        /// <summary>
        /// 匿名类的初始化器
        /// </summary>
        private static void AnonymousInitializer()
        {
            var pet = new { Age = 10, Name = "Maiomiao" };
            var students = new List<StudentName> {
                new StudentName { FirstName = "Wang", LastName = "Meimei" },
                new StudentName { FirstName = "Li", LastName = "Leilei" }
            };
            var studentsFrom = new List<StudentFrom> {
                new StudentFrom { FirstName = "Wang", City = "Beijing" },
                new StudentFrom { FirstName = "Li", City = "Hefei" }
            };
            var joinQuery = from s in students
                            join f in studentsFrom on s.FirstName equals f.FirstName
                            select new { FirstName = s.FirstName, LastName = s.LastName, City = f.City };
            foreach (var item in joinQuery)
            {
                Console.WriteLine("{0},{1},{2}", item.FirstName, item.LastName, item.City);
            }
        }

        /// <summary>
        /// 集合的初始化器
        /// </summary>
        private static void CollectionInitializer()
        {
            var students = new List<StudentName> {
                new StudentName("Wang", "Meimei"),
                new StudentName { FirstName = "Li", LastName = "Leilei" },
                new StudentName { Id = 3 },
                new StudentName("Zhang", "Xiaoshuai") { Id = 4 },
                null
            };
            foreach (var item in students)
            {
                if (item != null)
                {
                    Console.WriteLine(item.ToString());
                }
            }

            Dictionary<int, StudentName> dic = new Dictionary<int, StudentName>() {
                {11, new StudentName{ FirstName = "Li", LastName = "Leilei" } },
                {12, new StudentName{ FirstName = "Wang", LastName = "Meimei" } },
                {13, new StudentName("Zhang", "Xiaoshuai") { Id = 4 } }
            };
            foreach (var item in dic)
            {
                Console.WriteLine(item.Value.ToString());
            }
        }
    }
    
    public class StudentName
    {
        public string FirstName { get; set; }
        
        public string LastName { get; set; }

        public int Id { get; set; }

        public StudentName()
        { }

        public StudentName(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }

        public override string ToString()
        {
            return Id + ":" + FirstName + " - " + LastName;
        }
    }

    public class StudentFrom
    {
        public string FirstName { get; set;}

        public string City { get; set;}
    }
}